package com.stormrage.edu.serv.exam.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.constants.ErrCons;
import com.stormrage.edu.dao.exam.ExmScorePercentDao;
import com.stormrage.edu.serv.exam.ExmScorePercentServ;
import com.stormrage.entity.exam.ExmScorePercent;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @Title: ExamScorePercentServImpl.java
 * @Package com.stormrage.edu.serv.exam.impl
 * @Description: 专业分数比例 Service
 * @author www.stormragetech.com
 * @date 2016年4月22日
 * @version V1.0
 */
@Service
public class ExmScorePercentServImpl implements ExmScorePercentServ {

	private static final Logger logger = LoggerFactory.getLogger(ExmScoreServImpl.class);

	@Autowired
	private ExmScorePercentDao examScorePercentDao;

	@Override
	public Integer create(final ExmScorePercent arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExamScorePercentServ 】- 【 create 】-【 STARTED 】");

		Integer index = examScorePercentDao.create(arg0);

		logger.debug(" 【 Serv 】- 【 ExamScorePercentServ 】- 【 create 】-【 COMPLETED 】");
		return index;

	}

	@Override
	public void update(final ExmScorePercent arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExamScorePercentServ 】- 【 update 】-【 STARTED 】");

		if (!Integer.valueOf(arg0.getExam() + arg0.getOper() + arg0.getUsua()).equals(100))
			throw StormrageTechException.instance(ErrCons.SERV_ERR, "设置的百分比之和应为100%", StormrageTechException.ERR_APP,
					"【 ExamScorePercentServ 】- 【 update 】");

		ExmScorePercent entity = queryByTermId(arg0.getTermId());

		if (null == entity)
			create(arg0);
		else {
			arg0.setId(entity.getId());
			examScorePercentDao.update(arg0);
		}

		logger.debug(" 【 Serv 】- 【 ExamScorePercentServ 】- 【 update 】-【 COMPLETED 】");

	}

	@Override
	public ExmScorePercent queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExamScorePercentServ 】- 【 queryById 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		ExmScorePercent entity = examScorePercentDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 ExamScorePercentServ 】- 【 queryById 】-【 COMPLETED 】");
		return entity;

	}

	@Override
	public ExmScorePercent queryByTermId(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 ExamScorePercentServ 】- 【 queryByTermId 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("termId", arg0);

		ExmScorePercent entity = examScorePercentDao.queryByTermId(map);

		logger.debug(" 【 Serv 】- 【 ExamScorePercentServ 】- 【 queryByTermId 】-【 COMPLETED 】");
		return entity;

	}

}
